import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {List, Brand, Detail} from './DTO';
const baseUrl = '/afterSales/api/v1';

import {dutyUnitStatus, dutyUnitType} from '../Enum';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '索赔供应商'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const startDesc = formatMessage({
    id: 'api.message.startDesc',
    defaultMessage: '启用{resourceName}'
}, {resourceName});

const stopDesc = formatMessage({
    id: 'api.message.stopDesc',
    defaultMessage: '停用{resourceName}'
}, {resourceName});

const brandDesc = formatMessage({
    id: 'api.message.brandDesc',
    defaultMessage: '查询品牌'
});

const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询供应商详情'
});
const productCategoryQueryDesc = formatMessage({
    id: 'api.message.productCategoryQueryDesc',
    defaultMessage: '查询车型信息'
});
// 查询
export const getList = queryCondition => {
    const queryString = stringify({
        ...queryCondition,
        type: dutyUnitType.索赔供应商
    });
    return request(`${baseUrl}/dutyUnits?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/dutyUnits/${id}`, null, Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
// 新增
export const addInfo = data => request(`${baseUrl}/dutyUnits`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        ...data,
        type: dutyUnitType.索赔供应商
    })
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));


// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/dutyUnits/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        ...data,
        type: dutyUnitType.索赔供应商
    })
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 启用
export const startInfo = (id, data) => request(`${baseUrl}/dutyUnits/${id}/start`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, startDesc));
// 停用
export const stopInfo = (id, data) => request(`${baseUrl}/dutyUnits/${id}/stop`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, stopDesc));

// 获取品牌
export const getBrands = () => {
    const queryCondition = stringify({
        status: dutyUnitStatus.生效
    });
    return request(`${baseUrl}/brands?${queryCondition}`, null, Brand)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, brandDesc));
};
export const productCategoryQuery = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/productCategories?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, productCategoryQueryDesc));
};
